* Reset settings and initialize log file
launch, path("share/unconnected")

*-------------------------------------------------------------------------------
* Price and Wasserman (2024), "The Summer Drop in Female Employment"
*
* Description: Estimate seasonality in employment in rotation groups 5-8 among *              respondents who didn't work in education during rotations 1-4.
*-------------------------------------------------------------------------------


* Prepare data and run models
*-------------------------------------------------------------------------------

if "$estimate" != "0" {
	* Restrict the sample to those present in rotation groups 1 to 4
	gzuse "$basepath/data/derived/cps_bms_sample.dta.gz", clear
	bysort pid (tm): keep if _N >= 4 & mish[4] == 4

	* Restrict to those with valid longitudinal links throughout
	keep if linked_complete == 1

	* Create three measures of involvement with ed or ed-adjacent jobs
	gen byte ed1 = (school == 1)
	gen byte ed2 = (school == 1 | inrange(occ1990, 113, 163))
	gen byte ed3 = (school == 1 | inrange(occ1990, 113, 163) | ind1990 == 862)

	* Tag individuals connected to education in the first year
	bysort pid (tm): gen byte ever_ed1 = (ed1[1] | ed1[2] | ed1[3] | ed1[4])
	bysort pid (tm): gen byte ever_ed2 = (ed2[1] | ed2[2] | ed2[3] | ed2[4])
	bysort pid (tm): gen byte ever_ed3 = (ed3[1] | ed3[2] | ed3[3] | ed3[4])

	* Restrict to observations in rotation groups 5 to 8
	keep if inrange(mish, 5, 8)

	* Store the sample
	tempfile core
	save `core', replace

	* Estimate specifications
	foreach f of numlist 0 1 {
		* Loop over sample restrictions
		forvalues k = 0/4 {
			if `k' == 0 local kcond "1"
			if `k' == 1 local kcond "ever_ed1 == 0"
			if `k' == 2 local kcond "ever_ed2 == 0"
			if `k' == 3 local kcond "ever_ed3 == 0"
			if `k' == 4 local kcond "ever_ed1 == 1"

			* Reload sample
			use if female == `f' & `kcond' using `core', clear
			gcollapse (mean) emp (rawsum) wtfinl (first) month tmspline* weeks [pw = wtfinl], by(tm)
			quietly tsset tm

			* Run specifications
			quietly ivreg2 emp ib5.month tmspline* weeks [aw = wtfinl], bw($bandwidth) robust small
			process_estimates, path("unconnected") model("f`f'_k`k'")
		}
	}
}


* Prepare estimates
*-------------------------------------------------------------------------------

* Load estimates into memory
load_estimates, path("unconnected")

* Prepare coefficient labels for each month
make_coeflabels


* Plot EPOP in successively narrower samples
*-------------------------------------------------------------------------------

* Prepare background shading
clear
set obs 14
gen rlow = -2
gen rupp = 1
gen rval = _n - 0.5

* Plot estimates
foreach f of numlist 0 1 {
	if `f' == 0 local flbl "men"
	if `f' == 1 local flbl "women"

	#delimit ;
	coefplot
		(f`f'_k0, offset(-.15) recast(connected))
		(f`f'_k1, offset(-.05) recast(connected))
		(f`f'_k2, offset(+.05) recast(connected))
		(f`f'_k3, offset(+.15) recast(connected)),
		coeflabels(`coeflabels')
		title("")
		xtitle("")
		xlabel(, alternate)
		ytitle("Difference relative to May (p.p.)")
		yscale(range(-2 1))
		ylabel(-2(0.5)1, format(%5.1f))
		addplot(
			rarea rupp rlow rval if inrange(rval, 0.5, 5.5), color($ltgs) plotregion(margin(t=0 b=0)) below ||
			scatteri 0 0.5 0 13.5, recast(line) color(black) below)
		vertical
		legend(rows(2) size(*1) bmargin(0 0 0 2) keygap(1.5) order(
			4 "Was present in previous year"
			6 "+ wasn't in education sector"
			8 "+ wasn't a teacher"
			10 "+ wasn't in daycare sector"))
		rescale(100);
	#delimit cr

	nicepdf "$basepath/output/unconnected_`flbl'.pdf", indirect replace
}

* Report the point estimate
estimates replay f1_k1

* Close the log file
unlaunch
